<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="../../assets/css/maincss.css" rel="stylesheet" />
<link href="../../assets/css/but.css" rel="stylesheet" />
</head>
<script type="text/javascript" src="../../assets/js/jquery-1.11.1.js"></script>
<script type="text/javascript"
	src="../../javascript/My97DatePicker/WdatePicker.js"></script>
<body>
<h3>修改订单详情</h3>

<table id="model" style="display: none">
		<tr>
			<td>
				<select  name="products">
					<option value="0">选择商品</option>
				</select>
			</td>
			<td>
				<input id="quantity" name="quantity" type="number" />
			</td>
			<td>
				<select id="discount" name="discount">
					<option value="0">不打折</option>
					<option value="0.95">95折</option>
					<option value="0.85">85折</option>
				</select>
			</td>
			<td>
				<button name="del" value="del" class="but red">删除</button>
			</td>
		</tr>
	</table>

<div>
	<form id="myform">
		<div>
			订单编号:<input id="orderId" name="orderId" type="text" readonly /> 
			下单时间:<input id="orderTime" name="orderTime" type="text" onclick="WdatePicker()"> 
			<select id="customers" name="customers">
				<option value="0">选择客户</option>
			</select> 
			<select id="employees" name="employees">
				<option value="0">选择员工</option>
			</select>
		</div>
		<br />
		<table border="1px" cellspacing="0" width="900px" align="center" style="text-align: center;" rules="rows" frame="void">
			<tr>
				<td>产品名称</td>
				<td>数量</td>
				<td>是否折扣</td>
				<td>操作</td>
			</tr>
			<tr>
				<td colspan="4">
					<input type="button" class="but green" style="width: 65px;border: none;" name="add" value="增加数据" />
					&nbsp;&nbsp;&nbsp;
					<input type="button" class="but green" style="width: 90px;border: none;" name="sub" value="提交修改订单" />
				</td>
			</tr>
		</table>
	</form>
					<!-- <button name="add" value="add" class="but green" style="width: 65px;">增加数据</button> -->
					<!-- &nbsp;&nbsp;&nbsp; -->
					<!-- <button name="sub" value="sub" class="but green" style="width: 65px;">提交订单</button> -->
</div>
</body>

<script type="text/javascript">
	
	// 获取到url上获取 的orderID值
	function getParams(key) {
	    var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
	    var r = window.location.search.substr(1).match(reg);
	    if (r != null) {
	        return unescape(r[2]);
	    }
	    return null;
	};

	var orderID= getParams("orderID");
	// alert(orderID);
	
	// 准备修改订单详情的数据[员工,客户,商品]
	$(function(){
		$.ajax({
			type: "GET",
			url: "OrderDetailServlet",
			data : "op=readyEditDetail",
			dataType: "JSON",
			success: function(allResult){
				// alert(allResult);
				// 员工
				var $empArr = allResult[0];
				for(var i = 0;i < $empArr.length;i++){
					$("select[name='employees']").append("<option value='"+$empArr[i].empID+"'>"+$empArr[i].empName+"</option>");
				}
				// 客户
				var $custArr = allResult[1];
				for(var i = 0;i < $custArr.length;i++){
					$("select[name='customers']").append("<option value='"+$custArr[i].customerID+"'>"+$custArr[i].customerName+"</option>");
				}
				// 商品
				var $prodArr = allResult[2];
				for(var i = 0;i < $prodArr.length;i++){
					$("select[name='products']").append("<option value='"+$prodArr[i].productId+"'>"+$prodArr[i].productName+"</option>");
				}
			},
			error:function(){}
		});
	});
	
	
	// 订单详情
	// $(function(){
		// alert(orderID);
		$.ajax({
			type: "GET",
			url: "OrderDetailServlet",
			data : {
				"op" : "showDetailByOrderID",
				"orderID" : orderID
			},
			dataType: "JSON",
			success: function(detailResult){
				// alert(detailResult);
				// 订单编号 时间
				$("input[id='orderId']").val(detailResult[0].order.orderID);
				$("input[id='orderTime']").val(detailResult[0].order.orderDate);
				
				// 遍历客户数据
				var $custArr = $("#customers").find("option");
				for (var i = 0; i < $custArr.length; i++) {
					alert($custArr.eq(i).val());
				}
				
				/* if(detailResult[0].order.customer.customerID == $custArr.eq(i).val()){
		        	// $custArr.eq(i).attr("selected","selected");
		        	alert(detailResult[0].order.customer.customerID+"    "+$custArr.eq(i).val()+"    "+detailResult[0].order.customer.customerID == $custArr.eq(i).val());
		        } */
			},
			error:function(){}
			
		});
	// });
	
	

	// 全屏点击事件
	$(document).click(function(e) {
		var op = e.target.value;
		// alert(op);
		if (op == "增加数据") {
			$("#model tr").clone().insertBefore("tr:last");
		} else if (op == "del") {
			$(e.target).parent().parent().remove();
		} else if (op == "提交订单") {
			
			// 数据提交前判断是否有重复数据
			var f = false;
			var arr = $("select[name='products']");
			for(var i = 0; i < arr.length; i++){
				for (var j = i + 1; j < arr.length; j++) {
					if(arr[i].value == arr[j].value){
						f = true;
						break;
					}
				}
			}
			if (f) {
				alert("所选商品中有重复,请重新选择.");
			} else {
				
				
			}
			
		}
	});




</script>


</html>